home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE17 / TIPTRIX / BITMANIP.PAS next >
Encoding:
Pascal/Delphi Source File  |  1996-12-19  |  2.4 KB  |  94 lines

  1. unit BitManip;
  2. // unit for bit manipulation
  3. ////////////////////////////////////////////////////////////////////////////////
  4. interface
  5. uses  Classes;
  6. type  TFakeBits = class
  7.       public
  8.        FSize: Integer;
  9.        FBits: Pointer;
  10.       end;
  11. type  tEnhancedBits     = class(tBits)
  12.        ffk              : tFakeBits;
  13.        constructor      Create;
  14.        procedure        RemoveBit(idx:integer);
  15.        procedure        InsertBit(idx:integer;b:boolean);
  16.       end;
  17. ////////////////////////////////////////////////////////////////////////////////
  18. implementation
  19. uses Consts;
  20. const BitsPerInt        = SizeOf(Integer) * 8;
  21.  
  22. constructor tEnhancedBits.Create;
  23. begin
  24.  inherited Create;
  25.  ffk:=tFakeBits(self);
  26. end;
  27.  
  28. procedure tEnhancedBits.RemoveBit(idx:integer);
  29. var   p,j,i             : integer;
  30.       tmp               : tEnhancedBits;
  31. begin
  32.  // check index
  33.  if (idx<0) or (idx>=size) then
  34.   raise EBitsError.CreateRes(SBitsIndexError);
  35.  // create temporary class
  36.  tmp:=tEnhancedBits.create;
  37.  // set size of temporary class
  38.  tmp.size:=size-1;
  39.  // load temporary class
  40.  j:=0;
  41.  for i:=0 to size-1 do if (i<>idx) then
  42.  begin
  43.   tmp[j]:=bits[i];
  44.   inc(j);
  45.  end;
  46.  // deallocate bit array of old class
  47.  Size:=0;
  48.  // set new size
  49.  Size:=tmp.Size;
  50.  // compute size of allocated bit array
  51.  p:=((tmp.Size + BitsPerInt - 1) div BitsPerInt)*sizeof(integer);
  52.  // move new array
  53.  move(tmp.ffk.FBits^,ffk.FBits^,p);
  54.  // free temporary class
  55.  tmp.free;
  56. end;
  57.  
  58. procedure tEnhancedBits.InsertBit(idx:integer;b:boolean);
  59. var   p,j,i             : integer;
  60.       tmp               : tEnhancedBits;
  61. begin
  62.  if (idx<0) or (idx>size) then
  63.   raise EBitsError.CreateRes(SBitsIndexError);
  64.  // create temporary class
  65.  tmp:=tEnhancedBits.create;
  66.  // set size of temporary class
  67.  tmp.size:=size+1;
  68.  // load temporary class
  69.  j:=0;
  70.  for i:=0 to size-1 do
  71.  begin
  72.   if i=idx then
  73.   begin
  74.    tmp[j]:=b;
  75.    inc(j);
  76.   end;
  77.   tmp[j]:=bits[i];
  78.   inc(j);
  79.  end;
  80.  if idx=size then tmp[j]:=b;
  81.  // deallocate bit array of old class
  82.  Size:=0;
  83.  // set new size
  84.  Size:=tmp.Size;
  85.  // compute size of allocated bit array
  86.  p:=((tmp.Size + BitsPerInt - 1) div BitsPerInt)*sizeof(integer);
  87.  // move new array
  88.  move(tmp.ffk.FBits^,ffk.FBits^,p);
  89.  // free temporary class
  90.  tmp.free;
  91. end;
  92. ////////////////////////////////////////////////////////////////////////////////
  93. end.
  94.